limsMaterialSecondaryLibrary.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.materialGrid,
            service: 'limsMaterialSecondaryLibraryServiceImpl',
            dbTable: 'T_LIMS_MATERIAL_SECONDARY_LIBRARY',
            columns: limsMaterialSecondaryLibrary.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
                        grid.refresh({
                            url: _this.urlManager.historyGrid + '/queries',
                            requestData: {
                                matSecondId_EQ: -1
                            }
                        })
                    })
                    Gikam.getAsyncComp('lims-material_secondary_inventory-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            page: true,
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
                    grid.refresh({
                        url: _this.urlManager.historyGrid + '/queries',
                        requestData: {
                            matSecondId_EQ: row.id
                        }
                    })
                })
                Gikam.getAsyncComp('lims-material_secondary_inventory-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: [{
                type: 'form',
                id: 'grouptype-select',
                columns: 1,
                fields: [{
                    field: 'groupType',
                    title: 'T_LIMS_MATERIAL_SECONDARY_LIBRARY.GROUPTYPE',
                    type: 'select',
                    search: true,
                    firstBlank: false,
                    refreshGrid: false,
                    onChange: function (field, value, index, oldValue, text) {
                        Gikam.getAsyncComp(_this.idManager.materialGrid).done(grid => {
                            grid.refresh({
                                url: limsMaterialSecondaryLibrary.baseUrl + '/queries',
                                requestData: {
                                    groupType_EQ: value
                                }
                            })
                        })
                    },
                    onBeforeSelect: function (value, rowIndex) {
                        let url = limsMaterialSecondaryLibrary.baseUrl + '/grouptype-selections'
                        var items = []
                        Gikam.postSync(url).done(rows => {
                            items = rows
                        })
                        return items
                    }
                }]
            }, {
                type: 'button',
                text: 'BASEMODULE.RM.MATERIALS.BUTTON_CHECKOUT',
                icon: 'export',
                onClick: function () {
                    var materialGrid = Gikam.getComp(_this.idManager.materialGrid)
                    var materialActivedRow = materialGrid.getActivedRow()
                    if (Gikam.isEmpty(materialActivedRow)) return
                    if (!materialActivedRow.status || materialActivedRow.status === '0') {
                        Gikam.alert('BASEMODULE.RM.MATERIALS.TIP.STATUS_IS_DISABLE')
                        return
                    }
                    var model = Gikam.create('modal', {
                        title: 'BASEMODULE.RM.MATERIALS.BUTTON_CHECKOUT',
                        height: '250',
                        width: '400',
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return
                            }
                        }
                    })
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'inventory-checkout-form',
                                titleWidth: 70,
                                fields: limsMaterialSecondaryLibrary.getStandardInventoryOutFields(true),
                                columns: 1,
                                onLoadSuccess: function () {
                                }
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    class: 'blue',
                                    icon: 'save',
                                    color: 'white',
                                    onClick: function () {
                                        var form = Gikam.getComp('inventory-checkout-form')
                                        if (form.validate()) {
                                            workspace.window.showMask();
                                            var data = form.getData()
                                            data.isoutoperation = '1'
                                            var url = _this.urlManager.materialGrid + '/checkout'
                                            Gikam.postText(url, Gikam.getJsonWrapper(data, [null, [materialActivedRow]])).done(req => {
                                                req = JSON.parse(req)
                                                if (req.message) {
                                                    Gikam.alert(req.message)
                                                } else {
                                                    materialGrid.refreshRowById(materialActivedRow.id)
                                                    Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
                                                        grid.refresh({
                                                            url: _this.urlManager.historyGrid + '/queries',
                                                            requestData: {
                                                                matSecondId_EQ: materialActivedRow.id
                                                            }
                                                        })
                                                    })
                                                    Gikam.getLastModal().close(data)
                                                }
                                            }).always(function () {
                                                workspace.window.closeMask();
                                            })
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close()
                                    }
                                }]
                            }]
                        }
                    })
                }
            }, {
                type: 'button',
                text: 'BASEMODULE.RM.MATERIALS.BUTTON_INVENTORY',
                icon: 'refresh',
                onClick: function () {
                    var materialGrid = Gikam.getComp(_this.idManager.materialGrid)
                    var materialActivedRow = materialGrid.getActivedRow()
                    if (Gikam.isEmpty(materialActivedRow)) return
                    if (!materialActivedRow.status || materialActivedRow.status === '0') {
                        Gikam.alert('BASEMODULE.RM.MATERIALS.TIP.STATUS_IS_DISABLE')
                        return
                    }
                    var model = Gikam.create('modal', {
                        title: 'BASEMODULE.RM.MATERIALS.T_LIMS_STANDARD_INVENTORY.MODAL_INVENTORY',
                        height: '250',
                        width: '400',
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return
                            }
                        }
                    })
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'refresh-inventory-form',
                                titleWidth: 70,
                                fields: limsMaterialSecondaryLibrary.getInventoryFormFields(),
                                columns: 1,
                                onLoadSuccess: function () {
                                }
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    class: 'blue',
                                    icon: 'save',
                                    color: 'white',
                                    onClick: function () {
                                        var form = Gikam.getComp('refresh-inventory-form')
                                        if (form.validate()) {
                                            workspace.window.showMask();
                                            var data = form.getData()
                                            var url = _this.urlManager.materialGrid + '/inventory'
                                            Gikam.postText(url, Gikam.getJsonWrapper(data, [null, [materialActivedRow]])).done(req => {
                                                req = JSON.parse(req)
                                                if (req.message) {
                                                    Gikam.alert(req.message)
                                                } else {
                                                    materialGrid.refreshRowById(materialActivedRow.id)
                                                    Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
                                                        grid.refresh({
                                                            url: _this.urlManager.historyGrid + '/queries',
                                                            requestData: {
                                                                matSecondId_EQ: materialActivedRow.id
                                                            }
                                                        })
                                                    })
                                                    Gikam.getLastModal().close(data)
                                                }
                                            }).always(function () {
                                                workspace.window.closeMask();
                                            })
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close()
                                    }
                                }]
                            }]
                        }
                    })
                }
            }]
        }
    },

    getInventoryTab: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.inventoryGrid,
            service: 'limsMaterialSecondaryInventoryServiceImpl',
            dbTable: 'T_LIMS_MATERIAL_SECONDARY_INVENTORY',
            columns: limsMaterialSecondaryLibrary.getSecondInventoryListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
                        grid.refresh({
                            url: _this.urlManager.historyGrid + '/queries',
                            requestData: {
                                secondInventoryId_EQ: -1
                            }
                        })
                    })
                    Gikam.getAsyncComp('lims-material_secondary_inventory-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            // page: true,
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
                    grid.refresh({
                        url: _this.urlManager.historyGrid + '/queries',
                        requestData: {
                            secondInventoryId_EQ: row.id
                        }
                    })
                })
                Gikam.getAsyncComp('lims-material_secondary_inventory-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: [{
                type: 'button',
                text: 'BASEMODULE.RM.MATERIALS.BUTTON_CHECKOUT',
                icon: 'export',
                onClick: function () {
                    var materialGrid = Gikam.getComp(_this.idManager.materialGrid)
                    var materialActivedRow = materialGrid.getActivedRow()
                    if (Gikam.isEmpty(materialActivedRow)) return
                    if (!materialActivedRow.status || materialActivedRow.status === '0') {
                        Gikam.alert('BASEMODULE.RM.MATERIALS.TIP.STATUS_IS_DISABLE')
                        return
                    }
                    var grid = Gikam.getComp(_this.idManager.inventoryGrid)
                    var activedRow = grid.getActivedRow()
                    if (Gikam.isEmpty(activedRow)) return

                    if (!activedRow.ext$.matexpired || activedRow.ext$.matexpired === '1') {
                        Gikam.alert('BASEMODULE.RM.MATERIALS.TIP.USEFULTIME_PASSED')
                        return
                    }

                    var model = Gikam.create('modal', {
                        title: 'BASEMODULE.RM.MATERIALS.BUTTON_CHECKOUT',
                        height: '250',
                        width: '400',
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return
                            }
                        }
                    })
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'inventory-checkout-form',
                                titleWidth: 70,
                                fields: limsMaterialSecondaryLibrary.getStandardInventoryOutFields(true),
                                columns: 1,
                                onLoadSuccess: function () {
                                }
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    class: 'blue',
                                    icon: 'save',
                                    color: 'white',
                                    onClick: function () {
                                        var form = Gikam.getComp('inventory-checkout-form')
                                        if (form.validate()) {
                                            var data = form.getData()
                                            data.isoutoperation = '1'
                                            var url = _this.urlManager.inventoryGrid + '/checkout'
                                            Gikam.postText(url, Gikam.getJsonWrapper(data, [null, [activedRow]])).done(req => {
                                                req = JSON.parse(req)
                                                if (req.message) {
                                                    Gikam.alert(req.message)
                                                } else {
                                                    materialGrid.refreshRowById(materialActivedRow.id)
                                                    grid.refreshRowById(activedRow.id)
                                                    Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
                                                        grid.refresh({
                                                            url: _this.urlManager.historyGrid + '/queries',
                                                            requestData: {
                                                                secondInventoryId_EQ: activedRow.id
                                                            }
                                                        })
                                                    })
                                                    Gikam.getLastModal().close(data)
                                                }
                                            })
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close()
                                    }
                                }]
                            }]
                        }
                    })
                }
            }, {
                type: 'button',
                text: 'BASEMODULE.RM.MATERIALS.BUTTON_INVENTORY',
                icon: 'refresh',
                onClick: function () {
                    var materialGrid = Gikam.getComp(_this.idManager.materialGrid)
                    var materialActivedRow = materialGrid.getActivedRow()
                    if (Gikam.isEmpty(materialActivedRow)) return
                    if (!materialActivedRow.status || materialActivedRow.status === '0') {
                        Gikam.alert('BASEMODULE.RM.MATERIALS.TIP.STATUS_IS_DISABLE')
                        return
                    }
                    var grid = Gikam.getComp(_this.idManager.inventoryGrid)
                    var activedRow = grid.getActivedRow()
                    if (Gikam.isEmpty(activedRow)) return
                    var model = Gikam.create('modal', {
                        title: 'BASEMODULE.RM.MATERIALS.T_LIMS_STANDARD_INVENTORY.MODAL_INVENTORY',
                        height: '250',
                        width: '400',
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return
                            }
                        }
                    })
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'refresh-inventory-form',
                                titleWidth: 70,
                                fields: limsMaterialSecondaryLibrary.getInventoryFormFields(),
                                columns: 1,
                                onLoadSuccess: function () {
                                }
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    class: 'blue',
                                    icon: 'save',
                                    color: 'white',
                                    onClick: function () {
                                        var form = Gikam.getComp('refresh-inventory-form')
                                        if (form.validate()) {
                                            var data = form.getData()
                                            var url = _this.urlManager.inventoryGrid + '/inventory'
                                            Gikam.postText(url, Gikam.getJsonWrapper(data, [null, [activedRow]])).done(req => {
                                                req = JSON.parse(req)
                                                if (req.message) {
                                                    Gikam.alert(req.message)
                                                } else {
                                                    materialGrid.refreshRowById(materialActivedRow.id)
                                                    grid.refreshRowById(activedRow.id)
                                                    Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
                                                        grid.refresh({
                                                            url: _this.urlManager.historyGrid + '/queries',
                                                            requestData: {
                                                                secondInventoryId_EQ: activedRow.id
                                                            }
                                                        })
                                                    })
                                                    Gikam.getLastModal().close(data)
                                                }
                                            })
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close()
                                    }
                                }]
                            }]
                        }
                    })
                }
            }]
        }
    },

    getHistoryTab: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.historyGrid,
            service: 'limsMaterialSecondaryHistoryServiceImpl',
            dbTable: 'T_LIMS_MATERIAL_SECONDARY_HISTORY',
            columns: limsMaterialSecondaryLibrary.getSecondHistoryListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                }
            },
            // page: true,
            onRowActive: function (index, row) {
            },
            toolbar: []
        }
    },

    idManager: {
        materialGrid: 'lims-material-secondary-library-edit-list-grid',
        inventoryGrid: 'lims-material-secondary-inventory-edit-list-grid',
        historyGrid: 'lims-material-secondary-history-edit-list-grid'
    },

    urlManager: {
        materialGrid: limsMaterialSecondaryLibrary.baseUrl,
        inventoryGrid: IFM_CONTEXT + '/secure/basemodule/rm/material-secondary-inventorys',
        historyGrid: IFM_CONTEXT + '/secure/basemodule/rm/material-secondary-historys'
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-material-secondary-library-edit-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [_this.getGridParam()]
            },
            /*west: {
                width: '50%',
                title: 'T_LIMS_STANDARD_INVENTORY.SERVICE_NAME',
                items: [_this.getInventoryTab()],
            },*/
            center: {
                items: [{
                    type: 'tab',
                    panels: [{
                        title: 'T_LIMS_STANDARD_HISTORY.SERVICE_NAME',
                        items: [_this.getHistoryTab()],
                    }, {
                        title: 'GIKAM.FILE.ATTACHMENT',
                        id: 'lims-batch-special-uploader-panel',
                        items: [{
                            type: 'uploader',
                            id: 'lims-material_secondary_inventory-uploader',
                            dbTable: 'T_LIMS_MATERIAL_SECONDARY_LIBRARY',
                        }]
                    }]
                }]
            }
        })
    },

    init: function () {
        this.create()
    }
}
